/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ar.com.educacionit.vehiculos.entidades;

import ar.com.educacionit.base.entidades.Vehiculo;
import java.sql.*;
import java.util.*;

/**
 *
 * @author matias
 */
public class Auto extends Vehiculo {
    public Auto(){

    }

    private String marca;
    private String modelo;
    private String color;
    private double precio;
    private String equipamiento;
    private int id;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getEquipamiento() {
        return equipamiento;
    }

    public void setEquipamiento(String equipamiento) {
        this.equipamiento = equipamiento;
    }

    public String getMarca() {
        return marca;
    }

    public void setMarca(String marca) {
        this.marca = marca;
    }

    public String getModelo() {
        return modelo;
    }

    public void setModelo(String modelo) {
        this.modelo = modelo;
    }

    public double getPrecio() {
        return precio;
    }

    public void setPrecio(double precio) {
        this.precio = precio;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void insertar(Connection conn) throws Exception{
        Statement stmtInsert= conn.createStatement();
        String sql= "INSERT INTO autos (au_marca, au_modelo, au_precio, au_color, au_largo, au_ancho, au_altura, au_equipamiento) "
                + "VALUES ('"+getMarca()+"', '"+getModelo()+"', "+getPrecio()+", '"+getColor()+"', "+getLargo()+", "+getAncho()+", "+getAlto()+", '"+getEquipamiento()+"')";
        stmtInsert.execute(sql);
        stmtInsert.close();
    }

    public void modificar(Connection conn) throws Exception{
        Statement stmtUpdate= conn.createStatement();
        String sql= "UPDATE autos SET "
                + "au_marca= '"+getMarca()+"', "
                + "au_modelo= '"+getModelo()+"', "
                + "au_precio= "+getPrecio()+", "
                + "au_color= '"+getColor()+"', "
                + "au_largo= "+getLargo()+", "
                + "au_ancho= "+getAncho()+", "
                + "au_altura= "+getAlto()+", "
                + "au_equipamiento= '"+getEquipamiento()+"' "
                + "WHERE au_id= "+getId();
       stmtUpdate.execute(sql);
       stmtUpdate.close();
    }

    public void eliminar(Connection conn) throws Exception{
        Statement stmtDelete= conn.createStatement();
        String sql= "DELETE FROM autos WHERE au_id = "+ getId();
        stmtDelete.execute(sql);
        stmtDelete.close();
    }

    public ArrayList obtenerTodos(Connection conn) throws Exception{
        Statement stmtQuery= conn.createStatement();
        String sql= "SELECT * FROM autos";
        ResultSet rs= stmtQuery.executeQuery(sql);
        ArrayList aLista= new ArrayList();

        while(rs.next()){
            Auto a= new Auto();
            a.setId(rs.getInt("au_id"));
            a.setAlto(rs.getInt("au_altura"));
            a.setAncho(rs.getInt("au_ancho"));
            a.setLargo(rs.getInt("au_largo"));
            a.setMarca(rs.getString("au_marca"));
            a.setModelo(rs.getString("au_modelo"));
            a.setColor(rs.getString("au_color"));
            a.setPrecio(rs.getDouble("au_precio"));
            a.setEquipamiento(rs.getString("au_equipamiento"));
            aLista.add(a);
        }

        stmtQuery.close();
        return aLista;
    }
    
    public String toString(){
        return getMarca()+" - "+getColor()+" - "+getModelo();
    }
}
